package com.kongwg.java;

import org.junit.Test;

import java.util.*;

public class TreeMapTest {


    @Test
    public void test1() {
        TreeMap  map = new TreeMap();

        User u1 = new User("Tom", 18);
        User u2 = new User("Jerry", 11);
        User u3 = new User("Jack", 10);
        User u4 = new User("Rose", 17);
        User u5 = new User("Monika", 22);

        map.put(u1,98);
        map.put(u2,89);
        map.put(u3,76);
        map.put(u4,100);
        map.put(u5,79);


        Set entrySet = map.entrySet();

        Iterator iterator = entrySet.iterator();
        while (iterator.hasNext()){
            Object obj = iterator.next();

            Map.Entry entry = (Map.Entry) obj;

            System.out.println(entry.getKey() + "-->" + entry.getValue());
        }
    }



    @Test
    public void test2(){

        TreeMap  map = new TreeMap(new Comparator(){

            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof User && o2 instanceof User){
                    User u1 = (User) o1;
                    User u2 = (User) o2;

                    return Integer.compare(u1.getAge(),u2.getAge());
                }
                throw new RuntimeException("传入的数据类型不一致");
            }
        });

        User u1 = new User("Tom", 18);
        User u2 = new User("Jerry", 11);
        User u3 = new User("Jack", 10);
        User u4 = new User("Rose", 17);
        User u5 = new User("Monika", 22);

        map.put(u1,98);
        map.put(u2,89);
        map.put(u3,76);
        map.put(u4,100);
        map.put(u5,79);

        Set entrySet = map.entrySet();
        Iterator iterator = entrySet.iterator();
        while (iterator.hasNext()){
            Object obj = iterator.next();
            Map.Entry entry = (Map.Entry) obj;

            System.out.println(entry.getKey() + "----" + entry.getValue());
        }
    }

    @Test
    public void test8(){
        System.out.println("你好啊");
    }

}
